﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BasicCMS.Data.Tests
{
    [TestClass]
    public class UnitOfWorkTest
    {
        [TestMethod]
        public void test_Insert()
        {
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                unitOfWork.Insert("User").Value("Name", 1)
                    .Value("Password", 1);

                unitOfWork.Commit();
            }
        }

        [TestMethod]
        public void test_Update()
        {

            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                unitOfWork.Update("User")
                    .Set("Name", "Name")
                    .Set("Password", "Password")
                    .Where("Name=@Name")
                    .Param("Name", "1");

                unitOfWork.Commit();
            }
        }

        [TestMethod]
        public void test_InserAndUpdate()
        {
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                unitOfWork.Insert("User")
                    .Value("Name", 1)
                    .Value("Password", 1);

                unitOfWork.Update("User")
                    .Set("Name", "Name")
                    .Set("Password", "Password")
                    .Where("Name=@Name")
                    .Param("Name", "1");

                unitOfWork.Commit();
            }
        }

        [TestMethod]
        public void test_InserAndDelete()
        {
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                unitOfWork.Insert("User")
                    .Value("Name", 1)
                    .Value("Password", 1);

                unitOfWork.Delete("User").Where("Name=@Name")
                    .Param("Name", "Name");

                unitOfWork.Commit();
            }
        }

        [TestMethod]
        public void test_Execute()
        {
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                unitOfWork.Execute("DELETE FROM [User] WHERE Name=@Name").Param("Name", "1");
                unitOfWork.Commit();
            }
        }

        public class User
        {
            public int ID { get; set; }

            public string Password { get; set; }

            public string Name { get; set; }
        }

        [TestMethod]
        public void test_Query()
        {
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                var users=unitOfWork.Query("SELECT * FROM [User]").ToList<User>();
                Assert.IsTrue(users.Count > 0);
            }
        }

        [TestMethod]
        public void test_Paged_Query()
        {
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                var users = unitOfWork.Query("SELECT * FROM [User] ORDER BY ID").ToPagedList<User>(1,2);
                Assert.IsTrue(users.TotalPageCount > 0);
            }
        }

        [TestMethod]
        public void test_Paged_Table_Query()
        {
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                var users = unitOfWork.Table("User").OrderBy("ID").ToPagedTable(2, 2);
                Assert.IsTrue(users.TotalPageCount > 0);
            }
        }
    }
}
